﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Net;
using System.IO;

/// <summary>
/// Summary description for ImageLoader
/// </summary>
public class ImageLoader
{
    /*class ThreadParam
	{
    	public string	    m_szImagePath;
		public Rectangle	m_BoundingRect;
		CImageLoader	    m_pImageLoader;
		//CImageCellMngr	    m_pImageCellMngr;

		ThreadParam(string lpszImgPath, Rectangle boundingRect, ImageLoader pImageLoader, CImageCellMngr* pImageCellMngr)
		{
			m_szImagePath	= lpszImgPath;
			m_BoundingRect	= boundingRect;
			m_pImageLoader	= pImageLoader;
			//m_pImageCellMngr = pImageCellMngr;
		}
	};*/

    public string m_szServer;
	public uint	m_uPort;

	public ImageLoader()
    {	
    }


    /*LPCTSTR FilterImagePath(string lpszImgPath)
    {
	    return lpszImgPath + 25;
    }*/

    /*UINT ThreadFunction(LPVOID pParam)
    {
	    ThreadParam pThrdParam = (CThreadParam*)pParam;

        if(pThrdParam)
	    {
		    ImageLoader* pImgLoader = pThrdParam->m_pImageLoader;
		    CImageCellMngr* pImageCellMngr = pThrdParam->m_pImageCellMngr;

		    CImageCell* pImageCell = new CImageCell(pThrdParam->m_szImagePath, pThrdParam->m_BoundingRect);

		    if(!pImgLoader->Download(pThrdParam->m_szImagePath, pImageCell))
		    {
			    //Image* pImage = pImageCell->GetImage();
			    //pImage->Load(theApp.m_szAppPath + _T("\\sorry_noimage.jpg"));
			    pImageCell->Load(theApp.m_szAppPath + _T("\\sorry_noimage.jpg"));
		    }

		    pImageCellMngr->SyncAdd(pImageCell);

		    delete pThrdParam;
	    }

	    return 0;   // thread completed successfully
    }*/

    public void AsyncDownload(string szImgPath, Rectangle boundingRect, ImageCellMngr pImageCellMngr)
    {
	    //CThreadParam* pThrdParam = new CThreadParam(lpszImgPath, boundingRect, this, pImageCellMngr);
	    //m_ThrdList.Add(CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ThreadFunction, pThrdParam, 0, 0));
        ImageCell pImageCell = new ImageCell(szImgPath, boundingRect);

        if (!Download(szImgPath, pImageCell))
	    {
		    //pImageCell->Load(theApp.m_szAppPath + _T("\\sorry_noimage.jpg"));
	    }

	    pImageCellMngr.SyncAdd(pImageCell);
    }

    public bool Download(string szURL, ImageCell pImageCell)
    {
        WebClient Client = new WebClient();
        Stream data = Client.OpenRead(szURL);
        pImageCell.Load(data);
        data.Close();

	    /*CStdioFile* pHttpFile = NULL;

	    try
	    {
		    pHttpFile = m_Session.OpenURL(lpszFile);

		    if(pHttpFile)
		    {
			    IStream *pStream = NULL;
			    HRESULT hr = CreateStreamOnHGlobal(0, TRUE, &pStream);

			    ULARGE_INTEGER size;
			    size.QuadPart = 1024*32;
			    pStream->SetSize(size);

			    if(FAILED(hr))
			    {
				    pHttpFile->Close();      
				    delete pHttpFile;
				    return FALSE;
			    }

			    const int buffSize = 1024;
			    char buff[buffSize];
			    int nNumRead;

			    while((nNumRead = pHttpFile->Read(buff, buffSize)) > 0)
				    pStream->Write(buff, nNumRead, NULL);

			    pHttpFile->Close();      
			    delete pHttpFile;
			    pHttpFile = NULL;

			    //Create Image from stream

			    pImageCell->Load(pStream);
			    pStream->Release();

			    return TRUE;
		    }
	    }
	    catch (CInternetException* pEx) 
	    {
          // catch any exceptions from WinINet      
		    pEx->Delete();

		    if(pHttpFile)
			    delete pHttpFile;
	    }*/

	    return false;
    }

    /*void WaitForFinish()
    {
	    ::WaitForMultipleObjects(m_ThrdList.GetCount(), m_ThrdList.GetData(), TRUE, INFINITE);
	    m_ThrdList.RemoveAll();
    }*/


}
